import * as types from "../types/orderInfoTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";
import EnumConstants from "../common/EnumConstants";
import { fDateShow } from "../utils/utils";

const {
  OrderType, OrderStatus, ServiceTypeTexts, RecycleTypeFullTexts,
} = EnumConstants;

export function getOrderInfo(orderId, type) {
  return dispatch => {
    dispatch({ type: types.ORDERINFO_INIT });
    const params = {};
    let apiCode;
    if (type === OrderType.RECYCLE) {
      params.recycle_order_id = orderId;
      apiCode = ApiConstants.RECYCLEORDER_DETAIL;
    } else if (type === OrderType.SERVICE) {
      params.service_order_id = orderId;
      apiCode = ApiConstants.SERVICEORDER_DETAIL;
    } else {
      params.upgrade_order_id = orderId;
      apiCode = ApiConstants.UPDATEORDER_DETAIL;
    }
    NetWorkUtil.post(apiCode, params)
      .then(orderInfo => {
        const { order_status } = orderInfo;
        switch (order_status) {
          case OrderStatus.wait_subscribe:
            orderInfo.statustext = "已提交，等待确认…";
            // orderInfo.buttonText = "取消";
            orderInfo.buttonDisabled = false;
            break;
          case OrderStatus.wait_delivery:
            orderInfo.statustext = "预约成功，正在安排…";
            // orderInfo.buttonText = "取消";
            orderInfo.buttonDisabled = true;
            break;
          case OrderStatus.wait_pay:
            orderInfo.statustext = "已收货，等待支付…";
            orderInfo.buttonText = "支付";
            orderInfo.buttonDisabled = false;
            break;
          case OrderStatus.end:
            orderInfo.statustext = "维修/回收完成";
            break;
          default:
            break;
        }
        switch (type) {
          case OrderType.RECYCLE:
            orderInfo.orderTypeText = "回收";
            orderInfo.serviceTypeText = ServiceTypeTexts[String(orderInfo.recycle_type)];

            orderInfo.contactName = orderInfo.contact_name;
            orderInfo.contactMobile = orderInfo.collection_mobile;
            orderInfo.wayNameText = "回收方式";
            orderInfo.wayValueText = RecycleTypeFullTexts[orderInfo.recycle_type];
            orderInfo.showPrice = orderInfo.order_price;
            orderInfo.showTime = fDateShow(orderInfo.recycle_time);
            orderInfo.showAddress = orderInfo.transaction_address;
            break;
          case OrderType.UPDATE:
            orderInfo.orderTypeText = "升级";
            orderInfo.serviceTypeText = ServiceTypeTexts[String(orderInfo.upgrade_type)];

            orderInfo.contactName = orderInfo.real_name;
            orderInfo.contactMobile = orderInfo.mobile;
            orderInfo.wayNameText = "升级方案";
            orderInfo.wayValueText = orderInfo.upgrade_way;
            orderInfo.showPrice = orderInfo.upgrade_way_expense;
            orderInfo.showTime = fDateShow(orderInfo.upgrade_time);
            orderInfo.showAddress = `${orderInfo.province_name}-${orderInfo.city_name}-${orderInfo.county_name}${
              orderInfo.detail_address
            }`;
            break;
          case OrderType.SERVICE:
            orderInfo.orderTypeText = "维修";
            orderInfo.serviceTypeText = ServiceTypeTexts[String(orderInfo.service_type)];

            orderInfo.contactName = orderInfo.real_name;
            orderInfo.contactMobile = orderInfo.mobile;
            orderInfo.wayNameText = "维修方案";
            orderInfo.wayValueText = orderInfo.service_way;
            orderInfo.showPrice = orderInfo.service_expense;
            orderInfo.showTime = fDateShow(orderInfo.service_time);
            orderInfo.showAddress = `${orderInfo.province_name}-${orderInfo.city_name}-${orderInfo.county_name}${
              orderInfo.detail_address
            }`;
            break;
          default:
            break;
        }
        dispatch({ type: types.ORDERINFO_DOWN, orderInfo });
      })
      .catch(error => {
        toastShort(error.message);
        dispatch({ type: types.ORDERINFO_ERROR, error });
      });
  };
}

export function getStoreInfo() {
  return dispatch => {
    NetWorkUtil.post(ApiConstants.STOREADDRESS, {})
      .then(storeInfo => {
        dispatch({ type: types.STOREINFO_DOWN, storeInfo });
      })
      .catch(error => {
        toastShort(error.message);
        dispatch({ type: types.STOREINFO_ERROR, error });
      });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.ORDERINFO_UNMOUNT });
  };
}
